(ns com.richrail.interop.state
  (:gen-class
    :name com.richrail.state.ClojureStateProvider
    :prefix method-
    :implements [com.richrail.state.StateProvider])
  (:import java.util.ArrayList
           com.richrail.state.DepotState
           com.richrail.state.TrainState
           com.richrail.state.WagonState)
  (:use com.richrail.core))

(defn wagon->object [wagon]
  (WagonState. (obj-id wagon) (:seats wagon)))

(defn train->object [train]
  (TrainState. (obj-id train) (ArrayList. (reverse (map (fn [id] (wagon->object (find-object wagons @depot id))) (:twagons train))))))

(defn method-getState [_]
  (DepotState. (ArrayList. (map train->object (reverse (trains @depot))))
               (ArrayList. (map wagon->object (reverse (wagons @depot))))))